package py.com.erpone.session.dm;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import py.com.erpone.entity.dm.Laboratorio;
import py.com.erpone.entity.dm.Marca;



@Stateful
@Named
@LocalBean
@ConversationScoped
public class MarcaBean {

	@PersistenceContext(type=PersistenceContextType.EXTENDED)
	private EntityManager em;
	
	@Inject
	private Conversation conversation;
	
	private Marca marca;
	
	
	public MarcaBean(){
		
	}
	
	public Marca getMarca() {
		return marca;
	}
	public void setMarca(Marca marca) {
		this.marca = marca;
	}

	//agregar nuevo
	public void iniciar(){
		if(conversation.isTransient())
		conversation.begin();
		marca = new Marca();
	
	}
		
	//modificar
	public void iniciar(Marca marca){
		if(conversation.isTransient())
		conversation.begin();
		this.marca=em.find(Marca.class, marca.getId());
		
	}
		
	public void guardar(){
		if(marca.getId()==null)
		em.persist(marca);
		else
		em.merge(marca);
		if(!conversation.isTransient())
		conversation.end();
		
	}
		
	public void eliminar(Marca marca){
		em.remove(em.merge(marca));
		if(!conversation.isTransient())
		conversation.end();
	
	}
		
	public String cancelar(){
		if(!conversation.isTransient())
		conversation.end();
		return "/dm/marcas.xhtml";
	}
	
}
